package org.example.biz.impl;

import org.example.biz.ClaimVoucherBiz;
import org.example.oa.dao.ClaimVoucherDao;
import org.example.oa.dao.ClaimVoucherItemDao;
import org.example.oa.dao.DealRecordDao;
import org.example.oa.dao.EmployeeDao;
import org.example.oa.entity.ClaimVoucher;
import org.example.oa.entity.ClaimVoucherItem;
import org.example.oa.entity.DealRecord;
import org.example.oa.entity.Employee;
import org.example.oa.global.Contant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
/**
 * 业务层，调用dao层ClaimVoucherDao进行增删改查
 */
@Service("claimVoucherBiz")
public class ClaimVoucherBizImpl implements ClaimVoucherBiz {

    @Autowired
    private ClaimVoucherDao claimVoucherDao;
    @Autowired
    private ClaimVoucherItemDao claimVoucherItemDao;
    @Autowired
    private DealRecordDao dealRecordDao;

    public void save(ClaimVoucher claimVoucher, List<ClaimVoucherItem> items) {
        //设置创建时间
        claimVoucher.setCreateTime(new Date());
        //新创建的报销单的待处理人应为创建者，等待被提交
        claimVoucher.setNextDealSn(claimVoucher.getCreateSn());
        //报销单状态
        claimVoucher.setStatus(Contant.CLAIMVOUCHER_CREATED);
        //设置完成后保存到数据库
        claimVoucherDao.insert(claimVoucher);
        //把报销单保存到报销单条目,保存到数据库
        for(ClaimVoucherItem item : items){
            item.setClaimVoucherId(claimVoucher.getId());
            claimVoucherItemDao.insert(item);
        }
    }

    public ClaimVoucher get(int id) {

        return claimVoucherDao.select(id);
    }

    public List<ClaimVoucherItem> getItems(int cvid) {

        return claimVoucherItemDao.selectByClaimVoucher(cvid);

    }

    public List<DealRecord> getRecords(int cvid) {

        return dealRecordDao.selectByClaimVoucher(cvid);

    }

    public List<ClaimVoucher> getForSelf(String sn) {
        return claimVoucherDao.selectByCreateSn(sn);
    }

    public List<ClaimVoucher> getForDeal(String sn) {
        return claimVoucherDao.selectByNextDealSn(sn);
    }

    //更新报销单
    public void update(ClaimVoucher claimVoucher, List<ClaimVoucherItem> items) {
        claimVoucher.setNextDealSn(claimVoucher.getCreateSn());
        claimVoucher.setStatus(Contant.CLAIMVOUCHER_CREATED);
        claimVoucherDao.update(claimVoucher);
        //把报销单列表有出入的记录删掉
        List<ClaimVoucherItem> olds = claimVoucherItemDao.selectByClaimVoucher(claimVoucher.getId());
        for(ClaimVoucherItem old : olds){
            boolean isHave = false;
            for (ClaimVoucherItem item : items){
                if(item.getId() == old.getId()){
                    isHave =true;
                    break;
                }
            }
            if(!isHave){
                claimVoucherItemDao.delete(old.getId());
            }
        }
        //更新所有记录
        for(ClaimVoucherItem item : items){
            item.setClaimVoucherId(claimVoucher.getId());
            if(item.getId() != null && item.getId() > 0){
                //有就更新
                claimVoucherItemDao.update(item);
            }else {
                //没有就插入
                claimVoucherItemDao.insert(item);
            }
        }
    }

    @Autowired
    private EmployeeDao employeeDao;
    //
    public void submit(int id) {

        ClaimVoucher claimVoucher = claimVoucherDao.select(id);
        Employee employee = employeeDao.select(claimVoucher.getCreateSn());

        claimVoucher.setStatus(Contant.CLAIMVOUCHER_SUBMIT);
        claimVoucher.setNextDealSn(employeeDao.selectByDepartmentAndPost(employee.getDepartmentSn(), Contant.POST_FM).get(0).getSn());
        claimVoucherDao.update(claimVoucher);

        //保存记录
        DealRecord dealRecord = new DealRecord();
        dealRecord.setDealWay(Contant.DEAL_SUBMIT);
        dealRecord.setDealSn(employee.getSn());
        dealRecord.setClaimVoucherId(id);
        dealRecord.setDealResult(Contant.DEAL_SUBMIT);
        dealRecord.setDealTime(new Date());
        dealRecord.setComment("无");
        dealRecordDao.insert(dealRecord);
    }

    public void deal(DealRecord dealRecord) {
        ClaimVoucher claimVoucher = claimVoucherDao.select(dealRecord.getClaimVoucherId());
        Employee employee = employeeDao.select(dealRecord.getDealSn());
        //已通过
        if(dealRecord.getDealWay().equals(Contant.DEAL_PASS)){
            //金额小于限定值或处理人为总经理则不需要复审，交给财务
            if(claimVoucher.getTotalAmount() <= Contant.LIMIT_CHECK || employee.getPost().equals(Contant.POST_GM)){
                 claimVoucher.setStatus(Contant.CLAIMVOUCHER_APPROVED);
                 claimVoucher.setNextDealSn(employeeDao.selectByDepartmentAndPost(null, Contant.POST_CASHIER).get(0).getSn());

                 dealRecord.setDealTime(new Date());
                 dealRecord.setDealResult(Contant.CLAIMVOUCHER_APPROVED);
            }
            //需要复审
            else {
                claimVoucher.setStatus(Contant.CLAIMVOUCHER_RECHECK);
                claimVoucher.setNextDealSn(employeeDao.selectByDepartmentAndPost(null, Contant.POST_GM).get(0).getSn());

                dealRecord.setDealTime(new Date());
                dealRecord.setDealResult(Contant.CLAIMVOUCHER_RECHECK);
            }
        }
        //打回
        else if (dealRecord.getDealWay().equals(Contant.CLAIMVOUCHER_BACK)){
            claimVoucher.setStatus(Contant.CLAIMVOUCHER_RECHECK);
            claimVoucher.setNextDealSn(claimVoucher.getCreateSn());

            dealRecord.setDealTime(new Date());
            dealRecord.setDealResult(Contant.CLAIMVOUCHER_BACK);
        }
        //拒绝
        else if (dealRecord.getDealWay().equals(Contant.DEAL_REJECT)){
            claimVoucher.setStatus(Contant.CLAIMVOUCHER_TERMINATED);
            claimVoucher.setNextDealSn(null);

            dealRecord.setDealTime(new Date());
            dealRecord.setDealResult(Contant.CLAIMVOUCHER_TERMINATED);
        }
        //打款
        else if (dealRecord.getDealWay().equals(Contant.CLAIMVOUCHER_PAID)){
            claimVoucher.setStatus(Contant.CLAIMVOUCHER_TERMINATED);
            claimVoucher.setNextDealSn(null);

            dealRecord.setDealTime(new Date());
            dealRecord.setDealResult(Contant.CLAIMVOUCHER_PAID);
        }

        claimVoucherDao.update(claimVoucher);
        dealRecordDao.insert(dealRecord);
    }


}
